<?php
namespace Home\Controller;
use Think\Controller;
class IndexController extends Controller {
	private $url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential";
	//验证token
    public function index(){
		$timestamp = $_GET['timestamp'];  
		$nonce = $_GET['nonce'];  
		$token = "Jenson";  
		$signature = $_GET['signature'];  
		$array = array($timestamp,$nonce,$token);  
		sort($array);  

		//2.将排序后的三个参数拼接后用sha1加密  
		$tmpstr = implode('',$array);  
		$tmpstr = sha1($tmpstr);  

		//3. 将加密后的字符串与 signature 进行对比, 判断该请求是否来自微信  
		if($tmpstr == $signature)  
		{  
		echo $_GET['echostr'];  
			exit;  
		}  
    }
    //自定义菜单
    public function setMen(){
    	$MenUrl = "https://api.weixin.qq.com/cgi-bin/menu/create";
    	$assess_token = getToken($this->url , APPID , AppSecret);
    	$data = array(
    		'assess_token' => $assess_token['assess_token']
    	);
    	$result = returnPost($MenUrl , $data);
    	dump($result);
    }

    //查询自定义菜单
    public function readMen(){
    	$readMenUrl = "https://api.weixin.qq.com/cgi-bin/menu/get";
    	$assess_token = getToken($this->url , APPID , AppSecret);
    	$result  = file_get_contents($readMenUrl.'?access_token='.$assess_token['access_token']);
    	dump($readMenUrl.'?access_token='.$assess_token['access_token']);die;
    }

    //接收消息
    public function getMasg(){
    	$url ="https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=".APPID."&secret=".AppSecret;
    	$result = file_get_contents($url);
    	$result = json_decode($result , true);
    	$msgUrl = "https://api.weixin.qq.com/semantic/semproxy/search?access_token=".$result['assess_token'];
    	$data   = returnPost($msgurl);
    }
}